package com.codefox.controller;

import com.alibaba.fastjson.JSONObject;
import com.codefox.entity.TbDisease;
import com.codefox.service.TbDiseaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 处理前台搜索业务
 *
 * @author puma
 * @date 2018年9月26日10:28:36
 */
@Controller
@RequestMapping("/front")
public class DiseaseSearchFrontController {

    /**
     * IOC容器注入一个service层对象
     */
    private final
    TbDiseaseService service;

    /**
     * service对象注入实现代码
     *
     * @param service 注入对象
     */
    @Autowired
    public DiseaseSearchFrontController(TbDiseaseService service) {
        this.service = service;
    }

    /**
     * 前台首页搜索，用于跳转到搜索专用页面
     *
     * @param keyword 前台传入的搜索关键字
     * @return 转发到搜索专用页面
     */
    @RequestMapping("/search")
    public ModelAndView indexSearch(@RequestParam("keyword") String keyword) {
        System.out.println("keyword:" + keyword);
        return new ModelAndView("forward:searchResult.html", "keyword", keyword);
    }

    /**
     * 关键字搜索处理
     *
     * @param keyword 前台传入的关键字
     * @return 结果集的json
     */
    @ResponseBody
    @RequestMapping(value = "/resultSearch", produces = "application/json;charset=UTF-8")
    public Object resultSearch(@RequestParam("keyword") String keyword) {
        System.out.println("keyword:" + keyword);
        List<TbDisease> list = service.findByCondition(keyword);
        return JSONObject.toJSONString(list);
    }
    
    
    
}
